<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>{ Statement</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="ScoregensTop.html" title="Score Statements and GEN Routines" />
    <link rel="prev" href="y.html" title="y Statement (or Seed Statement)" />
    <link rel="next" href="rightbrace.html" title="} Statement" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">{ Statement</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="y.html">Prev</a> </td>
          <th width="60%" align="center">Score Statements and GEN Routines</th>
          <td width="20%" align="right"> <a accesskey="n" href="rightbrace.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="leftbrace"></a>
      <div class="titlepage"></div>
      <a id="IndexLeftBraceStatement" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">{ Statement</span>
        </h2>
        <p>{ — 
      Begins a non-sectional, nestable loop.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp212994000"></a>
        <h2>Description</h2>
        <p>
      The <span class="emphasis"><em>{</em></span> and <span class="emphasis"><em>} statements</em></span> can be used to repeat a group of score statements.  These loops do not constitute independent score sections and thus may repeat events within the same section.  Multiple loops may overlap in time or be nested within each other.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp213079072"></a>
        <h2>Syntax</h2>
        <pre class="synopsis"><span class="command"><strong>{</strong></span> p1 p2</pre>
      </div>
      <div class="refsect1">
        <a id="idp213081088"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>p1</em></span> -- Number of times to repeat the loop.
    </p>
        <p>
      <span class="emphasis"><em>p2</em></span> -- A macro name that is automatically defined at the beginning of the loop and whose value is advanced with each repetition (optional).  The initial value is zero and the final value is (p1 - 1).
    </p>
      </div>
      <div class="refsect1">
        <a id="idp213083712"></a>
        <h2>Performance</h2>
        <p>
      The <span class="emphasis"><em>{ statement</em></span> is used in conjunction with the <a class="link" href="rightbrace.html" title="} Statement"><em class="citetitle">} statement</em></a> to define repeating groups of other score events.  A score loop begins with the <span class="emphasis"><em>{ statement</em></span> which defines the number of repetitions and a unique macro name that will contain the current loop counter.  The body of a loop can contain any number of other events (including sectional breaks) and is terminated by a <span class="emphasis"><em>} statement</em></span> on its own line.  The <span class="emphasis"><em>} statement</em></span> takes no parameters.
    </p>
        <p>
      The use of the term "loop" here does not imply any sort of temporal succession to the loop iterations.  In other words, the p2 values of the events inside of the loop are not automatically incremented by the length of the loop in each repetition.  This is actually an advantage since it allows groups of simulataneous events to be easily defined as well.  The loop macro can be used along with <a class="link" href="ScoreEval.html" title="Evaluation of Expressions"><em class="citetitle"> score expressions</em></a> to increase the start times of events or to vary the events in any other way desired for each iteration.  The macro is incremented by one for each repetition.  Note that unlike the <span class="emphasis"><em>r statement</em></span>, the value of the macro the first time through the loop is zero (0), not one (1).  Therefore the final value is one less than the number of repetitions. 
    </p>
        <p>
      Score loops are a very powerful tool. While similar to the section repeat facility (the <a class="link" href="r.html" title="r Statement (Repeat Statement)"><em class="citetitle">r statement</em></a>), their chief advantage is that the score events in successive iterations of the loop are not separated by a section termination. Thus, it is possible to create multiple loops that overlap in time. Loops also can be nested within each other to a depth of 39 levels.
    </p>
        <div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">
          <table border="0" summary="Warning: Warning">
            <tr>
              <td rowspan="2" align="center" valign="top" width="25">
                <img alt="[Warning]" src="images/warning.png" />
              </td>
              <th align="left">Warning</th>
            </tr>
            <tr>
              <td align="left" valign="top">
                <p>
        Because of serious problems of interaction with macro expansion, loops must start and end in the same file, and not in a macro.
      </p>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div class="refsect1">
        <a id="idp213093568"></a>
        <h2>Examples</h2>
        <p>
      Here are some examples of the <span class="emphasis"><em>{</em></span> and <span class="emphasis"><em>} statements</em></span>.

      </p>
        <div class="example">
          <a id="idp213095408"></a>
          <p class="title">
            <strong>Example 1088. Sequentially repeat a three-note phrase four times.</strong>
          </p>
          <div class="example-contents"><pre class="programlisting">
<span class="stamnt">{</span> 4 CNT

<span class="stamnt">i</span>1  [0.00 + 0.75 * $CNT.]   0.2   220
<span class="stamnt">i</span>1  [0.25 + 0.75 * $CNT.]   .     440
<span class="stamnt">i</span>1  [0.50 + 0.75 * $CNT.]   .     880

<span class="stamnt">}</span>
        </pre>
      is interpreted as 
        <pre class="programlisting">
<span class="stamnt">i</span>1  0.00   0.2   220
<span class="stamnt">i</span>1  0.25   .     440
<span class="stamnt">i</span>1  0.50   .     880

<span class="stamnt">i</span>1  0.75   0.2   220
<span class="stamnt">i</span>1  1.00   .     440
<span class="stamnt">i</span>1  1.25   .     880

<span class="stamnt">i</span>1  1.50   0.2   220
<span class="stamnt">i</span>1  1.75   .     440
<span class="stamnt">i</span>1  2.00   .     880

<span class="stamnt">i</span>1  2.25   0.2   220
<span class="stamnt">i</span>1  2.50   .     440
<span class="stamnt">i</span>1  2.75   .     880
        </pre></div>
        </div>
        <p><br class="example-break" />
    </p>
        <p>
      </p>
        <div class="example">
          <a id="idp213109712"></a>
          <p class="title">
            <strong>Example 1089. Create a group of simultaneous harmonic partials.</strong>
          </p>
          <div class="example-contents">
        In this example, <span class="emphasis"><em>p4</em></span> is assumed to be the frequency of the note event.

        <pre class="programlisting">
<span class="stamnt">{</span> 8 PARTIAL
<span class="stamnt">i</span>1  0  1   [100 * ($PARTIAL. + 1)]
<span class="stamnt">}</span>
        </pre>
      is interpreted as 
        <pre class="programlisting">
<span class="stamnt">i</span>1  0  1   100
<span class="stamnt">i</span>1  0  1   200
<span class="stamnt">i</span>1  0  1   300
<span class="stamnt">i</span>1  0  1   400
<span class="stamnt">i</span>1  0  1   500
<span class="stamnt">i</span>1  0  1   600
<span class="stamnt">i</span>1  0  1   700
<span class="stamnt">i</span>1  0  1   800
        </pre></div>
        </div>
        <p><br class="example-break" />
    </p>
        <p>
       Here is a full example of the <span class="emphasis"><em>{</em></span> and <span class="emphasis"><em>} statements</em></span>. It uses the file <a class="ulink" href="examples/leftbrace.csd" target="_top"><em class="citetitle">leftbrace.csd</em></a>.

     </p>
        <div class="example">
          <a id="idp213122112"></a>
          <p class="title">
            <strong>Example 1090. An example of nested loops to create several inharmonic sine clusters.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in</span>
-odac           -iadc    <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o abs.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>
<span class="ohdr">nchnls</span> <span class="op">=</span> 2

gaReverbSend <span class="opc">init</span> 0

<span class="comment">; a simple sine wave partial</span>
<span class="oblock">instr</span> 1
    idur   <span class="op">=</span>       p3
    iamp   <span class="op">=</span>       p4
    ifreq  <span class="op">=</span>       p5
    aenv   <span class="opc">linseg</span>  0.0, 0.1<span class="op">*</span>idur, iamp, 0.6<span class="op">*</span>idur, iamp, 0.3<span class="op">*</span>idur, 0.0
    aosc   <span class="opc">oscili</span>  aenv, ifreq, 1
           <span class="opc">vincr</span>   gaReverbSend, aosc
<span class="oblock">endin</span>

<span class="comment">; global reverb instrument</span>
<span class="oblock">instr</span> 2
    al, ar <span class="opc">reverbsc</span> gaReverbSend, gaReverbSend, 0.85, 12000
           <span class="opc">outs</span>     gaReverbSend<span class="op">+</span>al, gaReverbSend<span class="op">+</span>ar
           <span class="opc">clear</span>    gaReverbSend
<span class="oblock">endin</span>

<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="stamnt">f</span>1 0 4096 10 1

{ 4 CNT
  { 8 PARTIAL
      <span class="comment">;   start time     duration            amplitude          frequency</span>
      
      <span class="stamnt">i</span>1  [0.5 * $CNT.]  [1 + ($CNT * 0.2)]  [500 + (~ * 200)]  [800 + (200 * $CNT.) + ($PARTIAL. * 20)]
  }
}

<span class="stamnt">i</span>2 0 6
<span class="stamnt">e</span>

<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1">
        <a id="idp213124064"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Gabriel Maldonado</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New in Csound version 3.52 (?).  (Fixed in version 5.08).</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="y.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="ScoregensTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="rightbrace.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">y Statement (or Seed Statement) </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> } Statement</td>
        </tr>
      </table>
    </div>
  </body>
</html>
